package com.elong.businesstravel.modules.member;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.android.support.jhf.edittextclear.EditTextClearView;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.onClick.OnClickNetworkStatusListener;
import com.android.support.jhf.utils.ValidateOperator;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.activity.BaseActivity;
import com.elong.businesstravel.base.utils.SaveUserOperate;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.protocol.login.MemberAddEmailRequest;
import com.elong.businesstravel.protocol.login.MemberAddEmailResponse;
import com.elong.businesstravel.protocol.login.MemberValidateEmailRequest;
import com.elong.businesstravel.protocol.login.MemberValidateEmailResponse;

/**
 * 邮箱认证
 * 
 * @author lenovo
 * 
 */
public class EmailAuthenticateActivity extends BaseActivity {

	protected EditTextClearView mEmailEditTextClearView;
	protected EditTextClearView mYzmEditTextClearView;
	protected Button mGetYzmButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_email_authenticate);

		init();
	}

	@Override
	protected void init() {

		mEmailEditTextClearView = (EditTextClearView) findViewById(R.id.emailEditTextClearView);
		mEmailEditTextClearView.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
		mEmailEditTextClearView.getEditText().setText(SaveUserOperate.getValidateEmali(mContext));
		
		mYzmEditTextClearView = (EditTextClearView) findViewById(R.id.yzmEditTextClearView);
		mYzmEditTextClearView.getEditText().setInputType(
				InputType.TYPE_CLASS_NUMBER);

		mGetYzmButton = (Button) findViewById(R.id.getYzmButton);

		setListener();
		setTitleNavBar();
	}

	@Override
	protected void setListener() {

		mGetYzmButton.setOnClickListener(new OnClickNetworkStatusListener(
				mContext) {

			@Override
			public void onClickNetworkSucceed(View view) {
				StringBuilder stringBuilder = new StringBuilder();
				if (!checkYZM(stringBuilder)) {
					HandlerToastUI.getHandlerToastUI(mContext,
							stringBuilder.toString());
				} else {
					// HandlerToastUI.getHandlerToastUI(mContext, "正在获取验证码");
					SaveUserOperate.setValidateEmali(mContext,
							mEmailEditTextClearView.getEditText().getText()
									.toString());
					memberAddEmail();
				}
			}

			@Override
			public void onClickNetworkFail(View view) {

			}
		});

		Button submitButton = (Button) findViewById(R.id.submitButton);
		submitButton.setOnClickListener(new OnClickNetworkStatusListener(
				mContext) {

			@Override
			public void onClickNetworkSucceed(View view) {
				StringBuilder stringBuilder = new StringBuilder();
				if (!checkSubmit(stringBuilder)) {
					HandlerToastUI.getHandlerToastUI(mContext,
							stringBuilder.toString());
				} else {
					// HandlerToastUI.getHandlerToastUI(mContext, "提交认证");
					memberValidateEmail();
				}
			}

			@Override
			public void onClickNetworkFail(View view) {

			}
		});
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("企业邮箱验证");
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	protected boolean checkYZM(StringBuilder errorStringBuilder) {
		if (TextUtils.isEmpty(mEmailEditTextClearView.getEditText().getText()
				.toString())) {
			errorStringBuilder.append("请输入邮箱");
			return false;
		}
		return true;
	}

	protected boolean checkSubmit(StringBuilder errorStringBuilder) {
		if (TextUtils.isEmpty(mEmailEditTextClearView.getEditText().getText()
				.toString())) {
			errorStringBuilder.append("请输入邮箱");
			return false;
		} else if (TextUtils.isEmpty(mYzmEditTextClearView.getEditText()
				.getText().toString())) {
			errorStringBuilder.append("请输入验证码");
			return false;
		} else if (4 != mYzmEditTextClearView.getEditText().getText()
				.toString().length()) {

			errorStringBuilder.append("请输入正确验证码");
			return false;
		}
		return true;
	}

	/**
	 * 从新获取验证码
	 */
	protected void againGetYzmRequest() {
		final ScheduledExecutorService scheduledExecutorService = Executors
				.newScheduledThreadPool(1);
		scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
			private int timeCount = 60;

			@Override
			public void run() {
				HandlerPostUI.getHandlerPostUI(new Runnable() {

					@Override
					public void run() {
						timeCount--;
						if (0 == timeCount) {
							mGetYzmButton.setText("获取验证码");
							scheduledExecutorService.shutdownNow();
							mGetYzmButton.setEnabled(true);
						} else {
							mGetYzmButton.setEnabled(false);
							mGetYzmButton.setText("重发(" + timeCount + ")");
						}
					}
				});
			}
		}, 1 * 1000, 1 * 1000, TimeUnit.MILLISECONDS);
	}

	private void memberAddEmail() {
		NetworkClient.getNetworkClient().PostRequest(
				new MemberAddEmailRequest(mContext, mEmailEditTextClearView
						.getEditText().getText().toString()),
				new MemberAddEmailResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						againGetYzmRequest();
						if ("0".equals(status)) {
							HandlerToastUI.getHandlerToastUI(mContext, "发送成功");
						} else {
							HandlerToastUI.getHandlerToastUI(mContext,
									messageString);
						}
					}
				});
	}

	private void memberValidateEmail() {
		NetworkClient.getNetworkClient().PostRequest(
				new MemberValidateEmailRequest(mContext, mYzmEditTextClearView
						.getEditText().getText().toString()),
				new MemberValidateEmailResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("0".equals(status)) {
							HandlerToastUI.getHandlerToastUI(mContext, "验证成功");
							setResult(RESULT_OK);
							finish();
						} else {
							HandlerToastUI.getHandlerToastUI(mContext,
									messageString);
						}
					}
				});
	}

}
